function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return decodeURIComponent(r[2]);
	}
	return null;
}

function showToast(msg, duration) {
	duration = isNaN(duration) ? 3000 : duration;
	var m = document.createElement('div');
	m.innerHTML = msg;
	m.setAttribute('class', 'toastDiv');
	document.body.appendChild(m);
	setTimeout(function () {
		var d = 0.5;
		m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
		m.style.opacity = '0';
		setTimeout(function () {
			document.body.removeChild(m);
		}, d * 1000);
	}, duration);
}


function checkMail() {
	var mail = document.getElementById("mail").value;
	if (!mail) {
		showToast("邮箱不能为空");
		mail = '';
		return false;
	}

	var emailReg = /^([a-zA-Z-z0-9_\.-]+)@([\da-zA-Z\.-]+)\.([a-zA-Z\.]{2,6})$/;
	if (!emailReg.test(mail)) {
		showToast("您输入的邮箱有误,请重新核对后再输入!");
		mail = '';
		return false;
	}

	return true;
}





function checkName() {
	var name = document.getElementById('name').value;

	if (!name || name == '') {
		name = '';
		showToast("姓名不能为空");
		return false;
	}
	var temp1 = name.match(/^[\u4e00-\u9fa5]{2,8}$/i);
	var temp2 = name.match(/^([A-Za-z]+\s?)*[A-Za-z]$/);
	var temp3 = name.match(/^[\u4e00-\u9fa5A-Za-z]{2,20}$/i);
	var isTrue = (temp1 || temp2 || temp3) && (name.length <= 20);
	if (!isTrue) {
		showToast("请输入正确的姓名");
		name = '';
		return false;
	}

	return true;
}



function checkPhone() {
	var phone = document.getElementById('cphone').value;

	if (!phone || phone == '') {
		showToast("手机号不能为空");
		phone = '';
		return false;
	}
	var telReg = /^(?:13\d|14\d|15\d|17\d|18\d)\d{5}(\d{3}|\*{3})$/;
	if (!telReg.test(phone)) {
		showToast("您输入的电话有误,请重新核对后再输入!");
		phone = '';
		return false;
	}

	return true;
}

function checkAge() {
	var age = document.getElementById('age_area').value;
	if (!age || age == '') {
		showToast("年龄不能为空");
		return false;
	}
	var telReg = /^(?:[1-9][0-9]?|1[01][0-9]|120)$/;
	if (!telReg.test(age)) {
		showToast("您输入的年龄有误");
		return false;
	}
	return true;
}

function registerCvt(_orderno) {
	var w = window,
		d = document,
		e = encodeURIComponent;
	var b = location.href,
		c = d.referrer,
		f, g = d.cookie,
		h = g.match(/(^|;)\s*ipycookie=([^;]*)/),
		i = g.match(/(^|;)\s*ipysession=([^;]*)/);
	if (w.parent != w) {
		f = b;
		b = c;
		c = f;
	};
	u = '//stats.ipinyou.com/cvt?a=' + e('uc.yB.tGsSmBvc-JCzSOHFvDpewP') + '&c=' + e(h ? h[2] : '') + '&s=' + e(i ? i[2].match(/jump\%3D(\d+)/)[1] : '') + '&u=' + e(b) + '&r=' + e(c) + '&rd=' + (new Date()).getTime() + '&OrderNo=' + e(_orderno) + '&e=';
	(new Image()).src = u;
}

function submit() {
	if (window.location.host.indexOf("192.168") > -1) {
		var form_api = '//172.16.233.50:70/VMD.OfficalApi/MgmLandingPageApi/MgmLandingPage';
	} else if (window.location.host.indexOf("stage") > -1) {
		var form_api = '//stageopenapi.vipabc.com/VMD/MGM.OfficalWebAPI/MgmLandingPageApi/MgmLandingPage';
	} else {
		var form_api = '//openapi.vipabc.com/VMD/MGM.OfficalWebAPI/MgmLandingPageApi/MgmLandingPage';
	}
	$.ajax({
		type: "POST",
		cache: false,
		url: form_api,
		data: {
			"ClientSn": clientSn,
			"fromwhere": fromwhere,
			"cmpName": '个人二维码',
			"edmName": '个人二维码',
			"edmSn": 'mgm2016_event07',
			"isdebug": isdebug,
			"name": $("#name").val(),
			"mail": $("#mail").val(),
			"sex": $("input[name='sex']:checked").val(),
			"cphone": $("#cphone").val(),
			"age_area": $("#age_area").val(),
			"relation": '个人二维码',
			"BrandId": 2
		},
		async: false,
		success: function (data) {
			if (data.int_insert_result == "1") {
				var jsid = data.jsuuid;
				registerCvt(jsid);
				$("#user_name1").html($('#name').val());
				$(".mask1").show();
			} else if (data.int_insert_result == "2") {
				var jsid = data.jsuuid;
				registerCvt(jsid);
				$("#user_name2").html($('#name').val());
				$(".mask2").show();
			} else {
				/*showToast("系统繁忙，请刷新重试");*/
				$("#user_name2").html($('#name').val());
				$(".mask2").show();
			}

		}
	});
}

var clientSn = getQueryString('ClientSn');
var fromwhere = getQueryString('fromwhere');
var isdebug = getQueryString('isdebug') ? getQueryString('isdebug') : 0;
$(function () {

	$("select").vSelect();
	$("input[type='radio']").parent().vRadio('', "#js-lp-form");
	$(".submit").click(function () {
		if (!checkName() || !checkAge() || !checkPhone() || !checkMail()) {
			return false;
		} else {
			submit();
		}
	});

});